/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd.point;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import who.tmap.bo.ThematicMap;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.point.CDPoint;
import who.tmap.bo.cd.point.CDPointBasic;
import who.tmap.bo.cd.point.CDPointDataDrivenSize;
import who.tmap.bo.cd.point.CDPointDataDrivenSymbol;
import who.tmap.bo.cd.point.CDPointDataDrivenSymbolAndSize;
import who.tmap.common.TMapException;
import who.tmap.toolkit.impl.AttrHelper;
import who.tmap.ui.cd.JPanelCDHelper;
import who.tmap.ui.cd.PanelCD;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JPanelCDPoint extends JPanel implements PanelCD, ComponentListener {

    private static Logger logger = Logger.getLogger(JPanelCDPoint.class); 
		
	private JPanel thisPanel =this;
	private ThematicMap thematicMap =null;
	private String joinIdColumnName =null;
	private String dataColumnName =null;
	private boolean dirty =false;
	
	private JPanelCDHelper jPanelCDHelper =new JPanelCDHelper();	

	private JLabel jLabelXColumn = null;
	private JComboBox jComboBoxXColumn = null;	
	private JLabel jLabelYColumn = null;
	private JComboBox jComboBoxYColumn = null;
	private JLabel jLabelMappingType = null;
	private JComboBox jComboBoxMappingType = null;
	
	private JPanelCDPointBasic basicPanel =new JPanelCDPointBasic();
	private JPanelCDPointDataDrivenSize dataDrivenSizePanel =new JPanelCDPointDataDrivenSize();
	private JPanelCDPointDataDrivenSymbol dataDrivenSymbolPanel =new JPanelCDPointDataDrivenSymbol();
	private JPanelCDPointDataDrivenSymbolAndSize dataDrivenSymbolAndSizePanel =new JPanelCDPointDataDrivenSymbolAndSize();
	private PanelCDPoint currentPanel =null;
	
	private JPanel jPanelTop = null;
	private JPanel jPanelCards = null;
	private JPanel jPanelBasic = null;

	private JLabel jLabelDescColumn = null;
	private JTextField jTextFieldDescColumn = null;
	private JComboBox jComboBoxDescColumn = null;
	private JLabel jLabelScalingFactor = null;
	private JTextField jTextFieldScalingFactor = null;

	/**
	 * This is the default constructor
	 */
	public JPanelCDPoint() {
		super();
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);

		this.add(getJPanelTop(), java.awt.BorderLayout.NORTH);
		this.add(getJPanelCards(), java.awt.BorderLayout.CENTER);
	}
	
	public PanelCDPoint getCurrentPanel() { 
		return currentPanel;
	}
	
	/**
	 * This method initializes jLabelData
	 * 
	 * @return javax.swing.JLabel
	 */
	private javax.swing.JLabel getJLabelYColumn() {
		if(jLabelYColumn == null) {
			jLabelYColumn = new javax.swing.JLabel();
			jLabelYColumn.setText("Y column");
		}
		return jLabelYColumn;
	}
	
	/**
	 * This method initializes jComboBoxData
	 * 
	 * @return javax.swing.JComboBox
	 */
	public JComboBox getJComboBoxYColumn() {
		if(jComboBoxYColumn == null) {
			jComboBoxYColumn = new javax.swing.JComboBox();
			jComboBoxYColumn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxYColumn;
	}
	
	/**
	 * This method initializes jLabelIdColumn
	 * 
	 * @return javax.swing.JLabel
	 */
	private JLabel getJLabelXColumn() {
		if(jLabelXColumn == null) {
			jLabelXColumn = new javax.swing.JLabel();
			jLabelXColumn.setText("X column");
		}
		return jLabelXColumn;
	}
	
	/**
	 * This method initializes jComboBoxData
	 * 
	 * @return javax.swing.JComboBox
	 */
	public JComboBox getJComboBoxXColumn() {
		if(jComboBoxXColumn == null) {
			jComboBoxXColumn = new javax.swing.JComboBox();
			jComboBoxXColumn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxXColumn;
	}
	
	/**
	 * This method initializes jLabelShape
	 * 
	 * @return javax.swing.JLabel
	 */
	private JLabel getJLabelMappingType() {
		if(jLabelMappingType == null) {
			jLabelMappingType = new javax.swing.JLabel();
			jLabelMappingType.setText("Mapping type");
		}
		return jLabelMappingType;
	}
	
	/**
	 * This method initializes jComboBoxShape
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxMappingType() {
		if(jComboBoxMappingType == null) {
			jComboBoxMappingType = new javax.swing.JComboBox( CDTypes.pointNames );
			
			jComboBoxMappingType.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if ( e.getStateChange() == java.awt.event.ItemEvent.SELECTED ) {
						CardLayout cl = (CardLayout)(getJPanelCards().getLayout());
					
						cl.show( getJPanelCards(), (String)e.getItem() );
						setDirty( true );
					}

				}
			});	

		}
		return jComboBoxMappingType;
	}
	
	public ThematicMap getThematicMap() {
		return thematicMap;
	}
	
	public void setThematicMap(ThematicMap thematicMap) {
		this.thematicMap = thematicMap;
		
		basicPanel.setThematicMap( thematicMap );
		dataDrivenSizePanel.setThematicMap( thematicMap );
		dataDrivenSymbolPanel.setThematicMap( thematicMap );
		dataDrivenSymbolAndSizePanel.setThematicMap( thematicMap );
	}
	
	public void refreshAttrColumnNames() {
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxXColumn() );
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxYColumn() );
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxDescColumn() );		
		
		jPanelCDHelper.populateJComboBox( thematicMap, dataDrivenSizePanel.getJComboBoxSizeColumn() );
		jPanelCDHelper.populateJComboBox( thematicMap, dataDrivenSymbolPanel.getJComboBoxDataColumn() );
		
		jPanelCDHelper.populateJComboBox( thematicMap, dataDrivenSymbolAndSizePanel.getJComboBoxDataColumn() );	
		jPanelCDHelper.populateJComboBox( thematicMap, dataDrivenSymbolAndSizePanel.getJComboBoxSizeColumn() );			
	}
	
	public void refreshData() {
		if ( thematicMap.getCartographicDisplay() instanceof CDPoint ) {
			CDPoint cd =(CDPoint)thematicMap.getCartographicDisplay();
			getJComboBoxXColumn().setSelectedItem( cd.getXColumn() );
			getJComboBoxYColumn().setSelectedItem( cd.getYColumn() );
			getJComboBoxDescColumn().setSelectedItem( cd.getDescriptorColumn() );			
			getJComboBoxMappingType().setSelectedIndex( cd.getCDPointType() - CDTypes.POINT_BASIC );
			getJTextFieldScalingFactor().setText( String.valueOf( cd.getScalingFactor() ) );

			PanelCDPoint panelCDPoint =null;
			
			if ( thematicMap.getCartographicDisplay() instanceof CDPointBasic ) {
				panelCDPoint =basicPanel;
			} else if ( thematicMap.getCartographicDisplay() instanceof CDPointDataDrivenSize ) {
				panelCDPoint =dataDrivenSizePanel;
			} else if ( thematicMap.getCartographicDisplay() instanceof CDPointDataDrivenSymbol ) {
				panelCDPoint =dataDrivenSymbolPanel;
			} else if ( thematicMap.getCartographicDisplay() instanceof CDPointDataDrivenSymbolAndSize ) {
				panelCDPoint =dataDrivenSymbolAndSizePanel;
			}
			
			panelCDPoint.refreshData();
		}
	}	
	
	public CartographicDisplay getCartographicDisplay() throws TMapException {
		CDPoint cd =(CDPoint)currentPanel.getCartographicDisplay();
		
		cd.setXColumn( (String)getJComboBoxXColumn().getSelectedItem() );
		cd.setYColumn( (String)getJComboBoxYColumn().getSelectedItem() );
		cd.setDescriptorColumn( (String)getJComboBoxDescColumn().getSelectedItem() );
		
		int scalingFactor =CDPoint.DEFAULT_SCALING_FACTOR;
		try {
			scalingFactor =Integer.parseInt( getJTextFieldScalingFactor().getText() );
		} catch( Exception e ) {
		}
		cd.setScalingFactor( scalingFactor );
		
		return cd;
	}

	/**
	 * This method initializes jPanelInfo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelTop() {
		if (jPanelTop == null) {
			jLabelScalingFactor = new JLabel();
			jLabelScalingFactor.setText("Scaling factor");
			jLabelDescColumn = new JLabel();
			jLabelDescColumn.setText("Descriptor column");
			GridLayout gridLayout = new GridLayout(5, 2, 5, 5);
			jPanelTop = new JPanel();
			jPanelTop.setLayout(gridLayout);

			jPanelTop.add(getJLabelXColumn(), null);
			jPanelTop.add(getJComboBoxXColumn(), null);
			jPanelTop.add(getJLabelYColumn(), null);
			jPanelTop.add(getJComboBoxYColumn(), null);
			jPanelTop.add(jLabelDescColumn, null);
			jPanelTop.add(getJComboBoxDescColumn(), null);
			jPanelTop.add(jLabelScalingFactor, null);
			jPanelTop.add(getJTextFieldScalingFactor(), null);
			jPanelTop.add(getJLabelMappingType(), null);
			jPanelTop.add(getJComboBoxMappingType(), null);
		}
		return jPanelTop;
	}

	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public String getDataColumnName() {
		return null;
	}
	
	public String getJoinIdColumnName() {
		return null;
	}
	
	public void setDataColumnName(String dataColumnName) {
	}

	public void setJoinIdColumnName(String joinIdColumnName) {
	}

	/**
	 * This method initializes jPanelCards	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCards() {
		if (jPanelCards == null) {
			jPanelCards = new JPanel();
			jPanelCards.setLayout( new CardLayout() );
			jPanelCards.add( basicPanel, CDTypes.pointNames[ CDTypes.POINT_BASIC - CDTypes.POINT_BASIC ]);
			basicPanel.addComponentListener( this );
			jPanelCards.add( dataDrivenSizePanel, CDTypes.pointNames[ CDTypes.POINT_DATA_DRIVEN_SIZE - CDTypes.POINT_BASIC ]);
			dataDrivenSizePanel.addComponentListener( this );
			jPanelCards.add( dataDrivenSymbolPanel, CDTypes.pointNames[ CDTypes.POINT_DATA_DRIVEN_SYMBOL - CDTypes.POINT_BASIC ]);
			dataDrivenSymbolPanel.addComponentListener( this );
			jPanelCards.add( dataDrivenSymbolAndSizePanel, CDTypes.pointNames[ CDTypes.POINT_DATA_DRIVEN_SYMBOL_AND_SIZE - CDTypes.POINT_BASIC ]);
			dataDrivenSymbolAndSizePanel.addComponentListener( this );
			
			currentPanel =basicPanel;
		}
		return jPanelCards;
	}

	public void componentResized(ComponentEvent e) {}

	public void componentMoved(ComponentEvent e) {}

	public void componentShown(ComponentEvent e) {
		currentPanel =(PanelCDPoint)e.getComponent();
	}

	public void componentHidden(ComponentEvent e) {}

	/**
	 * This method initializes jTextFieldDescColumn	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldDescColumn() {
		if (jTextFieldDescColumn == null) {
			jTextFieldDescColumn = new JTextField();
			jTextFieldDescColumn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jTextFieldDescColumn;
	}

	/**
	 * This method initializes jComboBoxDescColumn	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	public JComboBox getJComboBoxDescColumn() {
		if (jComboBoxDescColumn == null) {
			jComboBoxDescColumn = new JComboBox();
			jComboBoxDescColumn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxDescColumn;
	}

	/**
	 * This method initializes jTextFieldScalingFactor	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldScalingFactor() {
		if (jTextFieldScalingFactor == null) {
			jTextFieldScalingFactor = new JTextField();
			jTextFieldScalingFactor.setText( Integer.toString( CDPoint.DEFAULT_SCALING_FACTOR ) );
			jTextFieldScalingFactor.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					setDirty( true );
				}
			});
		}
		return jTextFieldScalingFactor;
	}
	
	/**
	 * Refresh entry tables that are dependent from CD Point Symbols entered on the map
	 */
	public void refreshEntryTables() {
		dataDrivenSymbolPanel.refreshTable();
		dataDrivenSymbolAndSizePanel.refreshTable();
	}
	
	public void applyEntryPointStructure() {
		refreshAttrColumnNames();
		
		getJComboBoxXColumn().setSelectedItem( AttrHelper.CD_POINT_X_COLUMN );
		getJComboBoxYColumn().setSelectedItem( AttrHelper.CD_POINT_Y_COLUMN );
		getJComboBoxDescColumn().setSelectedItem( AttrHelper.CD_POINT_DESC_COLUMN );	
		
		dataDrivenSizePanel.getJComboBoxSizeColumn().setSelectedItem( AttrHelper.CD_POINT_SIZE_COLUMN );
		dataDrivenSymbolPanel.getJComboBoxDataColumn().setSelectedItem( AttrHelper.CD_POINT_DESC_COLUMN );
		dataDrivenSymbolAndSizePanel.getJComboBoxSizeColumn().setSelectedItem( AttrHelper.CD_POINT_SIZE_COLUMN );
		dataDrivenSymbolAndSizePanel.getJComboBoxDataColumn().setSelectedItem( AttrHelper.CD_POINT_DESC_COLUMN );
		
//		try {
//			getCartographicDisplay();
//		} catch (TMapException e) {
//			logger.error( e );
//		}		
	}
		
}
